<?php
/**
* @file $Id: GetAllMP.fnc.php 398 2007-01-22 21:00:09Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function GetAllMP($mp,$marking_period_id='0')
{	global $_FOCUS;

	if($marking_period_id==0)
	{
		// there should be exactly one fy marking period unless the fy does not exist yet
		$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
		if(count($RET))
		{
			$marking_period_id = $RET[1]['MARKING_PERIOD_ID'];
			$mp = 'FY';
		}
		else
			return '0'; // MAB - this probably should be '' but make it '0' for now consistent with FYHACK's below
	}
	elseif(!$mp)
		$mp = GetMPTable(GetMP($marking_period_id,'TABLE'));

	if(!$_FOCUS['GetAllMP'][$mp])
	{
		switch($mp)
		{
			case 'PRO':
				// FYHACK - MAB - leave '0' in the FY list for now, remove later
				// there should be exactly one fy marking period
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				$fy = $RET[1]['MARKING_PERIOD_ID'];

				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				foreach($RET as $value)
				{
					$_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']] = "'0','$fy','$value[SEMESTER_ID]','$value[MARKING_PERIOD_ID]'";
					$_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']] .= ','.GetChildrenMP($mp,$value['MARKING_PERIOD_ID']);
					if(substr($_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']],-1)==',')
						$_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']] = substr($_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']],0,-1);
				}
			break;

			case 'QTR':
				// FYHACK - MAB - leave '0' in the FY list for now, remove later
				// there should be exactly one fy marking period
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				$fy = $RET[1]['MARKING_PERIOD_ID'];

				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				foreach($RET as $value)
					$_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']] = "'0','$fy','$value[SEMESTER_ID]','$value[MARKING_PERIOD_ID]'";
			break;

			case 'SEM':
				// FYHACK - MAB - leave '0' in the FY list for now, remove later
				// there should be exactly one fy marking period
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				$fy = $RET[1]['MARKING_PERIOD_ID'];

				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('SEMESTER_ID'));
				foreach($RET as $sem=>$value)
				{
					$_FOCUS['GetAllMP'][$mp][$sem] = "'0','$fy','$sem'";
					foreach($value as $qtr)
						$_FOCUS['GetAllMP'][$mp][$sem] .= ",'$qtr[MARKING_PERIOD_ID]'";
				}
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE NOT EXISTS (SELECT '' FROM SCHOOL_QUARTERS q WHERE q.SEMESTER_ID=s.MARKING_PERIOD_ID) AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				foreach($RET as $value)
					$_FOCUS['GetAllMP'][$mp][$value['MARKING_PERIOD_ID']] = "'0','$fy','$value[MARKING_PERIOD_ID]'";
			break;

			case 'FY':
				// FYHACK - MAB - leave '0' in the FY list for now, remove later
				// there should be exactly one fy marking period which better be $marking_period_id
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"),array(),array('SEMESTER_ID'));
				$_FOCUS['GetAllMP'][$mp][$marking_period_id] = "'0'";
				foreach($RET as $sem=>$value)
				{
					$_FOCUS['GetAllMP'][$mp][$marking_period_id] .= ",'$sem'";
					foreach($value as $qtr)
						$_FOCUS['GetAllMP'][$mp][$marking_period_id] .= ",'$qtr[MARKING_PERIOD_ID]'";
				}
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE NOT EXISTS (SELECT '' FROM SCHOOL_QUARTERS q WHERE q.SEMESTER_ID=s.MARKING_PERIOD_ID) AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
				foreach($RET as $value)
					$_FOCUS['GetAllMP'][$mp][$marking_period_id] .= ",'$value[MARKING_PERIOD_ID]'";
			break;
		}
	}

	return $_FOCUS['GetAllMP'][$mp][$marking_period_id];
}

function GetParentMP($mp,$marking_period_id='0')
{	global $_FOCUS;

	if(!$_FOCUS['GetParentMP'][$mp])
	{
		switch($mp)
		{
			case 'QTR':

			break;

			case 'SEM':
				$_FOCUS['GetParentMP'][$mp] = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID AS PARENT_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('MARKING_PERIOD_ID'));
			break;

			case 'FY':
				$_FOCUS['GetParentMP'][$mp] = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,YEAR_ID AS PARENT_ID FROM SCHOOL_SEMESTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('MARKING_PERIOD_ID'));
			break;
		}

	}

	return $_FOCUS['GetParentMP'][$mp][$marking_period_id][1]['PARENT_ID'];
}

function GetChildrenMP($mp,$marking_period_id='0')
{	global $_FOCUS;

	switch($mp)
	{
		case 'FY':
			if(!$_FOCUS['GetChildrenMP']['FY'])
			{
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('SEMESTER_ID'));
				foreach($RET as $sem=>$value)
				{
					$_FOCUS['GetChildrenMP'][$mp]['0'] .= ",'$sem'";
					foreach($value as $qtr)
						$_FOCUS['GetChildrenMP'][$mp]['0'] .= ",'$qtr[MARKING_PERIOD_ID]'";
				}
				$_FOCUS['GetChildrenMP'][$mp]['0'] = substr($_FOCUS['GetChildrenMP'][$mp]['0'],1);
			}
			return $_FOCUS['GetChildrenMP'][$mp]['0'];
		break;

		case 'SEM':
			if(GetMP($marking_period_id,'TABLE')=='SCHOOL_QUARTERS')
				$marking_period_id = GetParentMP('SEM',$marking_period_id);
			if(!$_FOCUS['GetChildrenMP']['SEM'])
			{
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('SEMESTER_ID'));
				foreach($RET as $sem=>$value)
				{
					foreach($value as $qtr)
						$_FOCUS['GetChildrenMP'][$mp][$sem] .= ",'$qtr[MARKING_PERIOD_ID]'";
					$_FOCUS['GetChildrenMP'][$mp][$sem] = substr($_FOCUS['GetChildrenMP'][$mp][$sem],1);
				}
			}
			return $_FOCUS['GetChildrenMP'][$mp][$marking_period_id];
		break;

		case 'QTR':
			return "'".$marking_period_id."'";
		break;

		case 'PRO':
			if(!$_FOCUS['GetChildrenMP']['PRO'])
			{
				$RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,QUARTER_ID FROM SCHOOL_PROGRESS_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('QUARTER_ID'));
				foreach($RET as $qtr=>$value)
				{
					foreach($value as $pro)
						$_FOCUS['GetChildrenMP'][$mp][$qtr] .= ",'$pro[MARKING_PERIOD_ID]'";
					$_FOCUS['GetChildrenMP'][$mp][$qtr] = substr($_FOCUS['GetChildrenMP'][$mp][$qtr],1);
				}
			}
			return $_FOCUS['GetChildrenMP'][$mp][$marking_period_id];
		break;
	}
}

function GetAllTerms($table)
{	global $_FOCUS;

	$mps_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM ".$table." ORDER BY SYEAR DESC,SORT_ORDER ASC"),array(),array('MARKING_PERIOD_ID'));
	foreach($mps_RET as $mp_id=>$mp)
	{
		$mp_list .= ",'$mp_id'";
	}
	$mp_list = substr($mp_list,1);
	
	return $mp_list;
}
?>